﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dao;
using System.Data.SqlClient;
namespace 班级系统
{
    public partial class Student_In : Form
    {
        Form frm;
        Student thou = new Student();
        public Student_In(Form ClassSelect, Student thou)
        {
            InitializeComponent();
            frm = ClassSelect;
            this.thou = thou;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
            frm.Show();
        }

        private void Student_In_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            listView1.Columns.Add("姓名", 68);
            listView1.Columns.Add("学号", 40);
            listView1.Columns.Add("年龄", 50);
            listView1.Columns.Add("性别", 60);
            listView1.Columns.Add("籍贯", 60);
            listView1.Columns.Add("考勤员", 60);
            string className = thou.Form1S[2].ToString();
            string sql = "select * from student where class_id="
                + "(select class_id from class where class_name=@className)";
            SqlParameter[] para = new SqlParameter[]{
            new  SqlParameter("className",className)
            };
            DataTable dt = sqlcontent.dt(sql, para);
            if (dt.Rows.Count == 0)
            {
                return;
            }
            foreach (DataRow myrow in dt.Rows)
            {
                //TODO 将考勤学生放在前四个
                string[] studentDetail ={
                    myrow["student_name"].ToString(),
                    myrow["student_num"].ToString(),
                    myrow["student_age"].ToString(),
                    myrow["student_sex"].ToString(),
                    myrow["student_place"].ToString(),
                    myrow["status"].ToString() 
                    };
                if (myrow["status"].ToString().Equals("1"))
                {
                    listView1.Items.Add(new ListViewItem(studentDetail));
                    continue;
                }
                else
                {
                    listView1.Items.Add(new ListViewItem(studentDetail));
                    continue;
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("请添加学生");
                return;
            }

            thou.listView = this.listView1;//直接赋值



            ClassSong frm = new ClassSong(this, thou);

            frm.Show();
            this.Hide();

        }

        private void button2_Click(object sender, EventArgs e)
        {

            int count = listView1.Items.Count;
            if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox3.Text == "" || textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("请输入完整信息");
                return;
            }
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (textBox1.Text == listView1.Items[i].Text || textBox2.Text == listView1.Items[i].SubItems[1].Text)
                {
                    MessageBox.Show("该学生已存在");
                    return;
                }

            }
            listView1.Items.Add(textBox1.Text);
            listView1.Items[count].SubItems.Add(textBox2.Text);
            listView1.Items[count].SubItems.Add(comboBox1.Text);
            listView1.Items[count].SubItems.Add(comboBox2.Text);
            listView1.Items[count].SubItems.Add(comboBox3.Text);
            listView1.Items[count].SubItems.Add("0");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("请先添加学生");
                return;
            }
            try
            {
                int x = listView1.SelectedIndices[0];
            }
            catch (Exception)
            {
                MessageBox.Show("请先选择学生");
                return;
            }
            listView1.SelectedItems[0].Remove();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 46)
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
            {
                e.Handled = false;
                return;
            }
            e.Handled = true;

        }
        private ListViewItem lit;
        private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++) { listView1.Items[i].Selected = false; }

            lit = e.Item;

            lit.Selected = true;


        }
        /// <summary>
        /// 搜索成员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.Items.Count; i++) { listView1.Items[i].BackColor = Color.White; }

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView1.Items[i].Text == textBox3.Text || listView1.Items[i].SubItems[1].Text == textBox3.Text)
                {
                    listView1.Items[i].Selected = true;
                    listView1.Items[i].BackColor = Color.Gainsboro;
                    return;
                }

            }
        }
    }
}
